package cn.com.yangzhenyu;

import java.util.ArrayList;
import java.util.List;

// static 代码块和普通构造函数 代码块的问题
public class Demo {

    private String name;

    public static String address;

    {
        name = "a";
        System.out.println(name);
    }

    static {
        address = "b";
        System.out.println(address);
    }

    Demo() {
        name = "c";
        System.out.println(name);
    }

    public static void main(String[] args) {
        Demo a = new Demo();
        List<String> l = a.generateParenthesis(6);
        for (String rs : l) {
            System.out.println(rs);
        }
    }

    public List<String> generateParenthesis(int n) {
        List<String> res = new ArrayList<String>();
        generate(res, "", 0, 0, n);

        return res;
    }

    //count1统计“(”的个数，count2统计“)”的个数
    public void generate(List<String> res, String ans, int count1, int count2, int n) {

        if (count1 > n || count2 > n) return;

        if (count1 == n && count2 == n) res.add(ans);


        if (count1 >= count2) {
            String ans1 = new String(ans);
            generate(res, ans + "(", count1 + 1, count2, n);
            generate(res, ans1 + ")", count1, count2 + 1, n);

        }
    }
}
